package com.ruoyi.classroom.course.controller;


import com.ruoyi.classroom.course.domain.Course;
import com.ruoyi.classroom.course.service.ICourseService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程设置管理Controller
 * 
 * @author lgm
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/course/course")
public class CourseController extends BaseController
{
    @Autowired
    private ICourseService courseService;

    /**
     * 查询课程设置管理列表
     */
//    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course)
    {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }
    /**
     * 查询所有老师
     */
    @RequiresPermissions("course:course:list")
    @GetMapping("/teacherList")
    public AjaxResult teacherList()
    {
        return success(courseService.selectTeacherList());
    }
    /**
     * 查询出所有教室
     */
    @RequiresPermissions("course:course:list")
    @GetMapping("/classroomList")
    public AjaxResult classroomList()
    {
        return success(courseService.selectClassroomList());
    }

    /**
     * 导出课程设置管理列表
     */
    @RequiresPermissions("course:course:export")
    @Log(title = "课程设置管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程设置管理数据");
    }

    /**
     * 获取课程设置管理详细信息
     */
    @RequiresPermissions("course:course:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程设置管理
     */
    @RequiresPermissions("course:course:add")
    @Log(title = "课程设置管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course)
    {
        int result = courseService.insertCourse(course);
        if (result == 0){
            return error("这个时间段内教室已经被占了~");
        }
        return toAjax(result);
    }

    /**
     * 修改课程设置管理
     */
    @RequiresPermissions("course:course:edit")
    @Log(title = "课程设置管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程设置管理
     */
    @RequiresPermissions("course:course:remove")
    @Log(title = "课程设置管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }
}
